package reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestClass {

	public static void main(String[] args) throws IllegalArgumentException,
			IllegalAccessException {
		Object[] objects = { new ClassX(), new ClassY(), new ClassX(),
				new ClassX()};
		checkArrayObjects(objects);
	}

	public static void checkArrayObjects(Object[] objects)
			throws IllegalArgumentException, IllegalAccessException {
		if (objects != null && objects.length > 0) {
			Object testObject = null;
			for (int i = 1; i < objects.length - 1; i++) {
				if (!(objects[i].getClass().equals(objects[i - 1].getClass()))) {
					if (objects[i].getClass().equals(objects[i + 1].getClass())) {
						System.out.println("Object at index " + (i - 1)
								+ " of type " + objects[i - 1].getClass()
								+ " is the different one");
						testObject = objects[i - 1];
						break;
					} else {
						System.out.println("Object at index " + (i)
								+ " of type " + objects[i].getClass()
								+ " is the different one");
						testObject = objects[i];
						break;
					}
				} else if (i == objects.length - 2) {
					System.out.println("Object at index "
							+ (objects.length - 1) + " of type "
							+ objects[objects.length - 1].getClass()
							+ " is the different one");
					testObject = objects[objects.length - 1];
				}
			}
			if (testObject != null) {
				Method[] methods = testObject.getClass().getMethods();
				for (Method m : methods) {
					if (m.toString().contains("goAway")) {
						System.out.println("method= " + m.getName().toString());
						return;
					}
				}
				for (Field field : testObject.getClass().getDeclaredFields()) {
					field.setAccessible(true);
					String name = field.getName();
					Object value = field.get(testObject);
					System.out.printf("Field name: %s, Field value: %s%n",
							name, value);
				}
			}
		}
	}

}
